package com.sms.util;

import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.net.URI;
import java.util.Properties;

import javax.swing.ImageIcon;

public class ResourcesHandler {

    public static String CONFIG_FILE = getCONFIG_FILE();

    public final static Image ICON = createImageIcon("icon.gif").getImage();
    public final static ImageIcon CANCEL_IMG = createImageIcon("Cancel.gif");
    public final static ImageIcon CONFIG_IMG = createImageIcon("Config.gif");
    public final static ImageIcon SEND_IMG = createImageIcon("Invia.gif");
    public final static ImageIcon SENS_SMS_IMG = createImageIcon("sendSms.png");
    public final static ImageIcon WEB_IMG = createImageIcon("Web.gif");
    public final static Dimension GUI_DIM = new Dimension(Toolkit
            .getDefaultToolkit().getScreenSize().width / 2, Toolkit
            .getDefaultToolkit().getScreenSize().height / 2);

    private static ImageIcon createImageIcon(String path) {
        try {
            return new ImageIcon("img/" + path);
        } catch (Exception e) {
            return null;
        }
    }

    private static String getCONFIG_FILE() {
        try {
            return new File(URI.create("config/configuration.properties")
                    .getPath()).getCanonicalPath();
        } catch (Exception e) {
            return null;
        }
    }

    public static void setUsername(String usr) {
        try {
            Properties p = new Properties();
            p.load(new FileReader(CONFIG_FILE));
            p.put("username", Base64.encode(usr));
            p.store(new FileWriter(CONFIG_FILE), "");
        } catch (Exception e) {
        }
    }

    public static String getUsername() {
        try {
            Properties p = new Properties();
            p.load(new FileReader(CONFIG_FILE));
            return Base64.decode(p.getProperty("username"));
        } catch (Exception e) {
            return null;
        }
    }

    public static void setPassword(String pwd) {
        try {
            Properties p = new Properties();
            p.load(new FileReader(CONFIG_FILE));
            p.put("password", Base64.encode(pwd));
            p.store(new FileWriter(CONFIG_FILE), "");
        } catch (Exception e) {
        }
    }

    public static String getPassword() {
        try {
            Properties p = new Properties();
            p.load(new FileReader(CONFIG_FILE));
            return Base64.decode(p.getProperty("password"));
        } catch (Exception e) {
            return null;
        }
    }
}